const express = require("express");
const pool = require("../pool.js");
//使用理由器
var router = express.Router();
//增加
router.post("/add", (req, res) => {
	var obj = req.body;
	var phone = obj.phone;
	var sql = "insert into user set ?"
	pool.query(sql, obj, (err, result) => {
		if(err) throw err;
		console.log(result);
		if(result.affectedRows > 0) {
			pool.query("SELECT * FROM user WHERE phone=?", phone, (error, data) => {
				if(error) throw error;
				data = JSON.stringify(data[0]);
				data = JSON.parse(data);
				pool.query("insert into basic set ?", {
					uid: data.uid
				}, (add_err, add_res) => {
					if(add_err) throw add_err;
				})
			})
			res.send({
				"code": 200,
				"msg": "添加成功"
			});
		} else {
			res.send({
				"code": 301,
				"msg": "添加失败"
			});
		}
	})
})

//查询手机号是否重复
router.get("/verification", (req, res) => {
	var _phone = req.query.phone;
	var sql = "SELECT * FROM user WHERE phone=?";
	pool.query(sql, _phone, (err, result) => {
		if(err) throw err;
		console.log(result);
		if(result.length > 0) {
			res.send({
				"code": 301,
				"msg": "手机号已注册"
			})
		} else {
			res.send({
				"code": 200,
				"msg": "手机号可用"
			});
		}
	})
})

//获取分页路由
router.get("/list", (req, res) => {
	var obj = req.query;
	var _page = obj.page;
	var _limit = obj.limit;
	if(!_page) {
		_page = 1;
	}
	if(!_limit) {
		_limit = 10;
	}
	//计算从第几条数据开始
	_page = (_page - 1) * _limit;
	//把字符串转化为整数
	_page = parseInt(_page);
	_limit = parseInt(_limit);

	var sql = "SELECT * FROM user LIMIT ?,?";
	pool.query(sql, [_page, _limit], (err, result) => {
		if(err) throw err;
		pool.query("SELECT COUNT(*) AS count FROM user;", (error, resu) => {
			if(error) throw error;
			resu = JSON.stringify(resu[0]);
			resu = JSON.parse(resu);
			var count = resu.count;
			res.send({
				"code": 0,
				"count": count,
				"data": result
			});
		})
	});
});
//删除
router.get("/delete", (req, res) => {
	var _uid = req.query.uid;
	var sql = "DELETE FROM user WHERE uid=?";
	pool.query(sql, _uid, (err, result) => {
		if(err) throw err;
		if(result.affectedRows > 0) {
			res.send({
				"code": 200,
				"msg": "删除成功"
			});
		} else {
			res.send({
				"code": 301,
				"msg": "删除失败"
			});
		}
	})
})
//获取详情
router.get("/details", (req, res) => {
	var _uid = req.query.uid;
	var sql = "SELECT * FROM user WHERE uid=?";
	pool.query(sql, _uid, (err, result) => {
		if(err) throw err;
		if(result.length) {
			res.send(result);
		} else {
			res.send({
				"code": 301,
				"msg": "查询失败"
			})
		}
	})
})
//编辑
router.post("/update", (req, res) => {
	var obj = req.body;
	console.log(obj);
	for(var key in obj) {
		if(!obj[key]) {
			res.send({
				"code": 301,
				"msg": key + "不能为空"
			})
			return;
		}
	}
	var _uid = obj.uid;
	delete obj.uid;
	var sql = "UPDATE user SET ? WHERE uid=?";
	pool.query(sql, [obj, _uid], (err, result) => {
		if(err) throw err;
		if(result.affectedRows > 0) {
			res.send({
				"code": 200,
				"msg": "修改成功"
			})
		} else {
			res.send({
				"code": 301,
				"msg": "修改失败"
			})
		}
	})
})
//导出路由器
module.exports = router;